package com.hundsun.s3.client.web.action;


import com.hundsun.s3.common.dto.DeptDTO;
import com.hundsun.s3.common.dto.Client;
import com.hundsun.s3.client.pub.dict.DictDept;
import com.hundsun.s3.pub.utils.BizSecurity;
import com.hundsun.s3.pub.utils.EmailUtil;
import com.hundsun.s3.client.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaowx23615 on 2018/2/2.
 */
@Controller
@RequestMapping("/user")
public class ClientAction {
    @Autowired
    private ClientService clientService;
    @Autowired
    private HttpSession session;
    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login.json",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(String username, String password, HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String, Object>();
        password = BizSecurity.desDecrypt(password, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
        password = BizSecurity.desEncrypt(password, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
        Client client = clientService.login(username,password);
        int result = clientService.judgeUsernameCancle(username);
        if(result != 0) {
            map.put("returnMsg","用户已注销");
            return map;
        }
        if(client == null){
            map.put("returnMsg","用户名或密码错误");
            return map;
        }
        session.setAttribute("username",client.getUsername());
        session.setAttribute("nickname",client.getNickname());
        map.put("returnMsg",client);
        return map;
    }

    /**
     * 判断用户是否已经存在
     * @param username
     * @return
     */
    @RequestMapping(value = "/judgeUsername.json",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> judgeUsername(String username) {
        Map<String,Object> map = new HashMap<String, Object>();
        int result = clientService.judgeUsername(username);
        map.put("isExist",result);
        return map;
    }

    /**
     * 注册
     * @param
     * @return
     */
    @RequestMapping(value = "/register.json",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> register(String username,String password,int sex,
                                       String nickname,String mailbox,String birthday,String phonenumber,String dept_tree,String verificationCode) {
        Map<String,Object> map = new HashMap<String, Object>();
        //加密
        password = BizSecurity.desEncrypt(password, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
//        if(session.getAttribute(mailbox).equals(verificationCode)) {
        String result = clientService.register(username,password,sex,nickname,mailbox,birthday,phonenumber,dept_tree);
//        if (result != null) {
//            map.put("returnMsg",result);
//        }else {
//            map.put("returnMsg","用户名或密码错误");
//        }
        map.put("returnMsg",result);

//        }else {
//            map.put("returnMsg","验证码输入错误");
//        }

        return map;
    }

    /**
     * 退出登陆
     * @return
     */
    @RequestMapping(value = "/logout.json")
    @ResponseBody
    public Map<String,Object> logout(String username,HttpServletRequest request) {
        session.removeAttribute("username");
        session.removeAttribute("nickname");
        Map<String,Object> map = new HashMap<String, Object>();
        return null;
    }

    /**
     * 邮件发送
     * @param to
     * @return
     */
    @RequestMapping(value = "/mail.json",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> sendMail(String to,HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String, Object>();
        String verificationCode = EmailUtil.getRandom();
        int isSuccess = EmailUtil.send(to,verificationCode);
        HttpSession session = request.getSession();
        session.setAttribute(to,verificationCode);
        map.put("returnMsg",isSuccess);
        return map;
    }



//
//    @RequestMapping(value = "/findPage.json",method = RequestMethod.GET)
//    @ResponseBody
//    public List<ClientPageDTO> findPage(int pageNo, int pageSize, String username, String nickname, String mailbox, String phonenumber, String dept_tree) {
//        return clientService.findPage(pageNo,pageSize,username,nickname,mailbox,phonenumber,dept_tree);
//    }

    /**
     * 判断邮箱是否已经注册
     * @param mailbox
     * @return
     */
    @RequestMapping("/judgeMailbox.json")
    @ResponseBody
    public Map<String,Object> judgeMailbox(String mailbox) {
        int returnCode = clientService.judgeMailbox(mailbox);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("isExist",returnCode);
        return map;
    }

//    @RequestMapping("/getAll.json")
//    @ResponseBody
//    public List<Message> getMessage(String fromId,String toId) {
//
////        return mongoTemplate.findAll(Message.class);
////        return clientService.readAllRecord(fromId,toId);
//        return mongoTemplate.findAll(Message.class);
//    }
//    @RequestMapping("/getUnread.json")
//    @ResponseBody
//    public List<Message> getUnread(String toId) {
//        return clientService.readRecord(toId);
//    }
//    @RequestMapping("/insert.json")
//    @ResponseBody
//    public void insert() {
//        Message message= new Message();
//        message.setRead(false);
//        message.setToName("xwx");
//        message.setToId("1");
//        clientService.saveRecord(message,false);
//    }


//    /**
//     * 删除记录
//     */
//    @RequestMapping("/remove.json")
//    @ResponseBody
//    public void remove() {
//        clientService.remove();
//    }
//
//    @RequestMapping("/link.json")
//    @ResponseBody
//    public List<LinkPeople> link(int client_id) {
//        return clientService.getLinkPeople(client_id);
//    }
//
//    /**
//     *
//     * @param username
//     * @return
//     */
    @RequestMapping("/client.json")
    @ResponseBody
    public Client getClientByUsername(String username) {
    	Client client = clientService.getClientByUsername(username);
    	List<DeptDTO> list = clientService.getDeptList();
    	String deptName = DictDept.build(list).getValue(client.getDept());
    	client.setDept(deptName);
        return client;
    }

    /**
     * 修改密码
     * @param username
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @RequestMapping("/resetPwd.json")
    @ResponseBody
    public Map<String,Object> modifyPwd(String username,String oldPwd,String newPwd) {
        Map<String,Object> map = new HashMap<String, Object>();
        String resultMsg = clientService.modifyPassword(username,oldPwd,newPwd);
        map.put("isSuccess",resultMsg);
        return map;
    }

}
